
/**
 * facebook UI Object
 * 
 */
var fbUI = {
	message: {unread: 0, all: 0},
	scope: {scope:'email,user_location,publish_actions'},
	//facebook login
	login: function(url) {
		FB.login(function(response) {
			if (response.authResponse) {
				$.post(url, response.authResponse, function(data) {
					window.location.reload(false);
				});
			}
		}, this.scope);
	},
	//facebook connect
	connect: function(url, callback) {
		FB.login(function(response) {
			if (response.authResponse) {
				$.post(url, response.authResponse, function(data) {
					callback(data);
				});
			}
		}, this.scope);
	},
	//invite friends: apprequests
	invite: function(title, message) {
		FB.login(function(response) {
			if (response.authResponse) {
				// user successfully logged in 
				FB.ui({
					method:'apprequests', link:'http://yamii.fi',
					title:title, message:message
				}, function (response) {
					_push({"key":"FB.ui::apprequests", "val":response});
				});
			}
		}, this.scope);
	},
	//recommend offer: send
	recommend: function(url) {
		FB.login(function(response) {
			if (response.authResponse) {
				FB.ui({
					method:'send', link:url
				}, function (response) {
					_push({"key":"FB.ui::send", "val":response});
				});
			}
		}, this.scope);
	},
	share: function(url) {
		window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(url), 'Share', 'toolbar=0, menubar=0, width=640, height=480');
	}
};

/**
 * YM global utils.
 */
var YM = {
	redeem: function(url, e) {
		$.ajax({'url':url,
			'beforeSend':function() {
				$(e).attr('disabled', true);
				$(e).before('<img id="img-loader" src="http://yamii.fi/img/loading-black.gif" /> ');
			},
			'success':function(data) {
				$('#img-loader').remove();
				$(e).attr('disabled', false);
				if (data.code !== 200) {
					alert(data.message);
				} else {
					$(e).hide();
					$('#stamped-icon').fadeIn();
					$('.offer_v .action .tips').html(data.message);
				}
			},
			'error': function() {
				$('#img-loader').remove();
				$(e).attr('disabled', false);
				alert('Whoops! We got this system error! Please try it again later.');
			}
		});
	}
};


// loading if document ready
$(function() {
	//set popup-window
	$('.popup-btn').facebox({
		loadingImage: '../img/ajax-loaders/ajax-loader-4.gif',
		closeImage: '../img/btn-close.png',
		overlayClose:true, width:360
	});
	//set tooltips
	$('.tooltip-onleft').tooltip({placement:'left'});
	$('.tooltip-onright').tooltip({placement:'right'});
	$('.tooltip-onbottom').tooltip({placement:'bottom'});
	$('.tooltip-ontop').tooltip({placement:'top'});
	//follow button
	$('.follow-place').click(function() {
		var btn = $(this);
		var param = null;
		if (btn.hasClass('active')) {
			param = {cancel:1};
			btn.removeClass('active');
		} else {
			btn.addClass('active');
		}
		$.get(btn.attr('href'),param,function(data){
			btn.html(data);
		});
		return false;
	});
});
function popup_window(e) {
	$(e).colorbox({transition:'none'});
	return false;
}

